package com.itheima.reggie.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.User;
import com.itheima.reggie.service.UserService;
import com.itheima.reggie.utils.ValidateCodeUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 手机短信验证码发送
     * @param user
     * @param request
     * @return
     */
    @PostMapping("/sendMsg")
    public R<String> sendMsg(@RequestBody User user, HttpServletRequest request) {
        log.info("手机号码: {}",user.getPhone());

        String phone = user.getPhone();

        if (StringUtils.isNotEmpty(phone)){
            //生成随机的4位验证码
            String code = ValidateCodeUtils.generateValidateCode(4).toString();
            log.info("code = : {}",code);

            //调用阿里云提供的短信服务API完成发送短信
            // SMSUtils.sendMessage("", "", phone, code);

            //需要将生成的验证码保存到Session
            request.getSession().setAttribute(phone,code);

            return R.success("验证码发送成功");
        }

        return R.error("验证码发送失败");
    }

    /**
     * 用户登录
     */
    @PostMapping("/login")
    public R<User> login(@RequestBody Map map,HttpServletRequest request){
        //1). 获取前端传递的手机号和验证码
        String phone = (String) map.get("phone");
        log.info("手机号: {}",phone);

        String code = (String) map.get("code");
        log.info("验证码: {}",code);
        //2). 从Session中获取到手机号对应的正确的验证码
        String codeInSession = (String) request.getSession().getAttribute(phone);

        //3). 进行验证码的比对 , 如果比对失败, 直接返回错误信息
        if (codeInSession != null && codeInSession.equals(code)){
            //4). 如果比对成功, 需要根据手机号查询当前用户, 如果用户不存在, 则自动注册一个新用户

            LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(User::getPhone,phone);

            User user = userService.getOne(queryWrapper);
            //判断账号是否存在
            if (user == null){
                String name = ValidateCodeUtils.generateValidateCode4String(5);
                //用户不参在,自动注册一个新用户
                user = new User();
                user.setPhone(phone);
                user.setStatus(1);
                user.setName(name);
                userService.save(user);
            }
            //5). 将登录用户的ID存储Session中
            request.getSession().setAttribute("user",user.getId());
            return R.success(user);
        }
        //验证码不正确登录失败
        return R.error("登录失败");
    }

    /**
     * 用户退出
     * @return
     */
    @PostMapping("/loginout")
    public R<String> loginout(HttpServletRequest request){
        request.getSession().removeAttribute("user");


        return R.success("用户退出成功");
    }
}
